from main import db
import datetime


class Tag(db.Model):
    __tablename__ = "tag"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    tag_name = db.Column(db.String(50), nullable=False)
    tag_url = db.Column(db.String(512), nullable=False)
    tag_icon = db.Column(db.String(10), nullable=True)

    tag_category_id = db.Column(db.Integer, db.ForeignKey("category.id"))
    tag_category = db.relationship('TagCategory', backref='tags', foreign_keys=[tag_category_id], lazy=True)

    tag_backup = db.Column(db.String(512), nullable=True)

    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    user = db.relationship('TagUser', backref='tags', foreign_keys=[user_id], lazy=True)

    create_at = db.Column(db.DateTime, default=datetime.datetime.now)

    def to_map(self):
        return {
            "id": self.id,
            "name": self.tag_name,
            "url": self.tag_url,
            "icon": self.tag_icon,
            "backup": self.tag_backup
        }
